package my;

import my.entity.Person;
import my.file.Files;
import my.printer.BinaryTrees;

import java.util.Comparator;

/**
 * <p>
 *
 * </p>
 *
 * @author AJun
 * @since 2020/8/13
 */
public class Main06 {

    public static void main(String[] args) {
        // test1();
        // test2();
        // test3();
        // test4();
        // test5();
        // test6();
        // test7();
        test8();
    }

    static void test1() {
        Integer[] data = {7, 4, 9, 2, 5, 8, 11, 3, 12, 1};

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (Integer n : data) bst.add(n);
        BinaryTrees.println(bst);
    }

    static void test2() {
        Integer[] data = {7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
        BinarySearchTree<Person> bst = new BinarySearchTree<>();
        for (Integer n : data) bst.add(new Person(n));
        BinaryTrees.println(bst);
    }

    static void test3() {
        Integer[] data = {7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
        BinarySearchTree<Person> bst = new BinarySearchTree<>(Comparator.comparingInt(Person::getAge));
        for (Integer n : data) bst.add(new Person(n));
        BinaryTrees.println(bst);
    }

    static void test4() {
        Integer[] data = {7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
        BinarySearchTree<Person> bst = new BinarySearchTree<>((o1, o2) -> o2.getAge() - o1.getAge());
        for (Integer n : data) bst.add(new Person(n));
        BinaryTrees.println(bst);
        String str = BinaryTrees.printString(bst);
        Files.writeToFile("chapter06/search_tree/Main06_test4", str);
    }

    static void test5() {
        Integer[] data = {7, 4, 2, 1, 3, 5, 9, 8, 11, 10, 12};

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (Integer n : data) bst.add(n);
        BinaryTrees.println(bst);

        // System.out.println("前序遍历递归: " + bst.preorderTraversal());
        // System.out.println("前序遍历非递归: " + bst.preorderTraversal(false));
        // System.out.println("中序遍历递归: " + bst.inorderTraversal());
        // System.out.println("中序遍历非递归: " + bst.inorderTraversal(false));
        // System.out.println("后序遍历递归: " + bst.postorderTraversal());
        // System.out.println("后序遍历非递归: " + bst.postorderTraversal(false));
        // System.out.println("层序遍历递归: " + bst.levelOrderTraversal());

        bst.levelOrder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public void visit(Integer element) {
                System.out.print(element + "\t");
            }
        });

        System.out.println();
        bst.preorder(element -> System.out.print(element + "\t"));
    }

    static void test6() {
        Integer[] data = {7, 4, 2, 1, 3, 5, 9, 8, 11, 10, 12};

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (Integer n : data) bst.add(n);
        // bst.preorder(System.out::println);
        // System.out.println(bst);

        BinaryTrees.println(bst);
        System.out.println("高度: " + bst.heightLevelOrder());

        bst.reverse();
        BinaryTrees.println(bst);
    }

    static void test7() {
        Integer[] data = {7, 4, 9, 2, 5};

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (Integer n : data) {
            bst.add(n);
        }
        BinaryTrees.println(bst);
        System.out.println(bst.isComplete());
    }

    static void test8() {
        Integer[] data = {7, 4, 9, 2, 5, 8, 11, 3, 12, 1};

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (Integer n : data) bst.add(n);

        BinaryTrees.println(bst);
        bst.inorder(element -> System.out.print(element + "\t"));
        System.out.println("\n\n");
        bst.remove(7);
        BinaryTrees.println(bst);
        bst.inorder(element -> System.out.print(element + "\t"));
    }

}
